<?php

/**
 * @copyright       : FOSP
 * @version         : 20120322
 * @name            : League
 * @todo            : League model
 * @author          : QuangTM
 */
class League
{
    const LEAGUE_DETAIL = 'league_detail';

    const LEAGUE_ALL = 'league_all';
    
    const LEAGUE_BY_GROUP = 'league_by_group_list';

    /**
     *
     * @var League
     */
    protected static $instance;

    protected function __construct()
    {
        
    }

    /**
     * Get League's instance
     * @return League
     */
    public static function getInstance()
    {
        if (!isset(self::$instance) || !self::$instance instanceof self)
            self::$instance = new self;
        return self::$instance;
    }

    /**
     * Get all list leagues
     * @return array|bool
     * @author QuangTM
     */
    public function getAll()
    {
        $memcacheInstance = Fpt_Global::getMemcacheInstance(1);
        $keyCache = Fpt_Global::makeCacheKey(self::LEAGUE_ALL);

        // Read cache
        $listAllLeagues = $memcacheInstance->read($keyCache);

        // Miss cache
        if ($listAllLeagues === FALSE)
        {
            try
            {
                $config = Fpt_Global::getApplicationIni();
                // Get db teamseason instance
                $leagueObj = Fpt_Business_League::factory($config['database']['default']);

                // Get data from db
                $listAllLeagues = $leagueObj->getAll();
                
                // Write to cache
                Fpt_Global::writeMemcache(1, $keyCache, $listAllLeagues);
//                $memcacheInstance->write($keyCache, $listAllLeagues);
            }
            catch (Exception $ex)
            {
                Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
                return FALSE;
            }
        }

        return $listAllLeagues;
    }
    
    /**
     * Get list league by ids
     * @param array $arrLeagueIDs
     * @return array
     */
    public function getListLeagueByIDs($arrLeagueIDs)
    {
        $result = array();
        $arrMissCache = array();
        foreach ($arrLeagueIDs as $leagueID)
        {
            $memcacheInstance = Fpt_Global::getMemcacheInstance($leagueID);
            $keyCache = Fpt_Global::makeCacheKey(self::LEAGUE_DETAIL, $leagueID);
            $leagueDetail = $memcacheInstance->read($keyCache);
            if ($leagueDetail === FALSE)
            {
                array_push($arrMissCache, $leagueID);
            }
            else
            {
                array_push($result, $leagueDetail);
            }
        }
        
        // If miss some items
        if (count($arrMissCache))
        {
            // Get config 
            $config = Fpt_Global::getApplicationIni();
            
            // Get DB Obj
            $leagueMySQL = Fpt_Business_League::factory($config['database']['default']);
            
            // Get multi article
            $arrMissLeagues = $leagueMySQL->getListLeagueByIDs($arrMissCache);
            
            // Write memcache for each
            foreach ($arrMissLeagues as $leagueDetail)
            {
//                $memcacheInstance = Fpt_Global::getMemcacheInstance($leagueDetail['LeagueID']);
                $keyCache = Fpt_Global::makeCacheKey(self::LEAGUE_DETAIL, $leagueDetail['LeagueID']);
//                $memcacheInstance->write($keyCache, $leagueDetail);
                Fpt_Global::writeMemcache($leagueDetail['LeagueID'], $keyCache, $leagueDetail);
            }
            
            $result = array_merge($result, $arrMissLeagues);
        }
        return $result;
    }
    
    public function getListLeagueByGroup($group)
    {
        $memcacheInstance = Fpt_Global::getMemcacheInstance($group);
        $keyCache = Fpt_Global::makeCacheKey(self::LEAGUE_BY_GROUP, array($group));
        // Read cache
        $listLeagues = $memcacheInstance->read($keyCache);

        // Miss cache
        if ($listLeagues === FALSE)
        {
            try
            {
                $config = Fpt_Global::getApplicationIni();
                // Get db teamseason instance
                $leagueObj = Fpt_Business_League::factory($config['database']['default']);

                // Get data from db
                $listLeagues = $leagueObj->getListLeagueByGroup($group);
                
                // Write to cache
//                $memcacheInstance->write($keyCache, $listLeagues);
                Fpt_Global::writeMemcache($group, $keyCache, $listLeagues);
            }
            catch (Exception $ex)
            {
                Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
                return FALSE;
            }
        }

        return $listLeagues;
    }

}